#define __main_c

#include "includeAll.h"


//0 默认时间，1最小时间，2最大时间
code const uint8_t functionArr[6][3]=
{
    {30,10,60}, //速热
    {70,30,120}, //养生茶
    {90,30,4 }, //甜品
    {90,30,4 }, //煮粥
    {100,30,6}, //炖汤
    {50,30,90}, //焖煮
    //{    }, //保温
};


code const uint8_t functionStepArr[6][10]=
{
	 {40,D_100,255,D_85  }, //速热
	 {40,D_100,20,D_85,255,D_60  },  //养生茶
	 {30,D_100,30,D_80,30,D_60,30,D_40,255,D_30  }, //甜品
	 {30,D_100,20,D_80,30,D_45,255,D_30  }, //煮粥
	 {30,D_100,30,D_80,30,D_60,30,D_40,255,D_30  }, //炖汤
	 {30,D_100,255,D_85  }, //焖煮
   
};
void main(void)
{
    CFGWL = (CFGWL & 0x3F) | 0x80;	//WDT enable in Fast/Slow mode,disable in Idle/Stop mode
    CLRWDT = 1;				//clear watchdog
    ChangeToFastClk();
    InitIoport ();
    InitRegister ();
    EA = 1;				//global interrupt enable
    Buzz(25);
    bStart=0;
    F_displayAll();

    while(1)
    {

        CLRWDT = 1;
        timerControl();
        GetKey();
        //DisplayNumber(888);
        //displayBuffer[4]=0xff;
		if((bAdOff==0)&&(bAdShort==0))
		{
			
		
        switch(keyValue)
        {
        case D_MoShi:
            KeyMoShi();

            break;

        case D_YuYue:
            KeyYuYue();
            break;

        case D_DingShi:
            KeyDingShi();
            break;

        case D_KaiGuan:
		//fireType++;
            KeyKaiGuan();
            break;
        }
        if(longKeyHoldTime>=8)
        {
            longKeyHoldTime=0;

            if(longKeyValue==D_DingShi)
            {
                KeyDingShi();
            }
            if(longKeyValue==D_YuYue)
            {
                KeyYuYue();
				
				if((bFunctionSure)&&(yuYueTime>0))
				{
					Buzz(25);
					yuYueTime=0;
					longKeyValue=0;
				}
            }
			


        }
		 LedDisplay();
		 keyValue=0;
		}

       

       
		if((bFunctionSure)&&(yuYueTime<=0))
		{
			if(function!=D_BaoWen)
			{
				OrdFunction();
			}
			else
			{
					bWattSetted=1;
					fireType=D_15;
			}
			
		}
		
		
		
    }

}

void KeyMoShi()
{
    if(((bPowerOnOff)&&(bFunctionSure==0))||(flashYuYueTime>0))
    {
		bPowerOnOff=1;
        bSecFlash500ms=1;
        timer500ms=0;
        flashFunction=5;
        flashYuYueTime=0;
        Buzz(25);
        if(function<7)
        {
            function++;
        }
        else
        {
            function=1;
        }
        workTime=functionArr[function-1][0];
        zuiXiaoWorkTime=functionArr[function-1][1];
        zuiDaWorkTime=functionArr[function-1][2];
        if((function==D_TianPin)||(function==D_ZhuZhou)||(function==D_DunTang))
        {
            zuiDaWorkTime=zuiDaWorkTime*60;
        }
    }
}


void KeyYuYue()
{
    if(bFunctionSure==0)
    {
		if(function!=0)
		{
			flashFunction=5;
		}
        bSecFlash500ms=1;
        timer500ms=0;
        if(longKeyValue==0)
        {
            Buzz(25);
        }
        flashYuYueTime=5;
        yuYueTime+=30;
        if(yuYueTime>570)
        {
            yuYueTime=30;
        }
    }
}


void KeyDingShi()
{
    if((bPowerOnOff)&&(bFunctionSure==0)&&(function!=D_BaoWen)&&(flashYuYueTime<=0))
    {
        bSecFlash500ms=1;
        timer500ms=0;
        if(longKeyValue==0)
        {
            Buzz(25);
        }

        flashFunction=5;
        if(workTime==zuiDaWorkTime)
        {
            workTime=zuiXiaoWorkTime;
        }
        else
        {
            if(longKeyValue==0)
            {
                workTime=workTime+5;
            }
            else
            {
                workTime=workTime+30;
            }
        }

        if(workTime>zuiDaWorkTime)
        {
            workTime=zuiDaWorkTime;
        }
    }
}


void KeyKaiGuan()
{
    Buzz(25);
	bSecFlash500ms=1;
        timer500ms=0;
    if((bPowerOnOff)||(flashYuYueTime>0))
    {
        PowerOff();
    }
    else
    {
        bPowerOnOff=1;
        flashFunction=5;
        function=1;
        workTime=functionArr[function-1][0];
        zuiXiaoWorkTime=functionArr[function-1][1];
        zuiDaWorkTime=functionArr[function-1][2];
    }
}

void  OrdFunction()
{
	if(step==0)
	{
		OrdStep(functionStepArr[function-1][step],functionStepArr[function-1][step+1]);
	}
	else if(step==1)
	{
		OrdStep(functionStepArr[function-1][step+1],functionStepArr[function-1][step+2]);
	}
	else if(step==2)
	{
		OrdStep(functionStepArr[function-1][step+2],functionStepArr[function-1][step+3]);
	}
	else if(step==3)
	{
		OrdStep(functionStepArr[function-1][step+3],functionStepArr[function-1][step+4]);
	}
	else if(step==4)
	{
		OrdStep(functionStepArr[function-1][step+4],functionStepArr[function-1][step+5]);
	}
	else if(step==5)
	{
		bWattSetted=1;
		bFinish=1;
		//Buzz(25);
		function=D_BaoWen;
		fireType=D_15;
	}
	
	
}


void OrdStep(uint8_t time1,uint8_t fireType1)
{
	if(bWattSetted==0)
	{
		bWattSetted=1;
		keepTime=time1;
	}
	fireType = fireType1;
	if(keepTime<=0)
	{
		step++;
		bWattSetted=0;
	}
	else if(workTime<=0)
	{
		step++;
		bWattSetted=0;
	}
}

void timerControl()
{
    if (timer40ms >= D_40ms)
    {
        timer40ms = 0;
       
            getTemp();
        

        if(bStart)
        {
            if((bAdOff==0)&&(bAdShort==0))
            {
				if(bFinish)
				{
					 F_displayOFF()  ;
				}
                else if(flashYuYueTime>0)
                {
                    if(bSecFlash500ms)
                    {
                        DisplayTime(yuYueTime);
                    }
                    else
                    {
                        F_displayClrAll();
                    }
                }
                else
                {
                    if(bPowerOnOff)
                    {
                        if((bFunctionSure)&&(yuYueTime>0))
                        {
                            if(bSecFlash2s)
                            {
                                DisplayTime(yuYueTime);
                            }
                            else
                            {
                                 F_displayClrAll();
                            }

                        }
                        else if(function!=D_BaoWen)
                        {
                            DisplayTime(workTime);
                        }
                        else
                        {
                            F_displayBaoWen() ;
                        }
                    }
                    else
                    {
                        F_displayPowerOff();
                    }

                }
            }
            else
            {
                if(bAdOff)
                {
                    if(bSecFlash500ms)
                    {
                       
                        F_displayAdOff()   ;
                    }
                    else
                    {
                        F_displayClrAll()   ;
                    }

                }
                if(bAdShort)
                {
                    if(bSecFlash500ms)
                    {
                        
                        F_displayAdShort();
                    }
                    else
                    {
                        F_displayClrAll()   ;
                    }
                }
            }
        }


        if(longKeyValue==0)
        {
            longKeyJudgeTime++;
        }
        else
        {
            longKeyHoldTime++;
        }


    }
    if (timer500ms >= D_500ms)
    {
        timer500ms = 0;
        bSecFlash500ms=~bSecFlash500ms;
		


    }
    if (timer1s >= D_1s)
    {
        timer1s = 0;
		if((bAdOff)||(bAdShort))
		{
			Buzz(25);
		}
		if((bFinish)&&(buzzNum<3))
		{
			buzzNum++;
			Buzz(25);
		}
        bUpdateCurTemp=1;
        if(flashFunction>0)
        {
            flashFunction--;
            if((flashFunction==0)&&(function!=0))
            {
                bFunctionSure=1;
                timer1m=0;
            }
        }
        if(flashYuYueTime>0)
        {
            flashYuYueTime--;
            if(flashYuYueTime==0)
            {
				if(function==0)
				{
					yuYueTime=0;
				}
                
            }

        }
        if(bStart==0)
        {
            F_displayPowerOff();
            bStart=1;
        }

        timer2s++;
        if(timer2s >= D_2s)
        {
            bSecFlash2s=~bSecFlash2s;
        }
        timer1m++;
        if(timer1m >= D_1m)
        {
            timer1m = 0;
			
            if(bFunctionSure)
            {
                if(yuYueTime>0)
                {
                    yuYueTime--;
					if(yuYueTime<=0)
					{
						Buzz(25);
					}
                }
				
				if(keepTime>0)
				{
					keepTime--;
				}

                if((workTime>0)&&(yuYueTime==0))
                {
                    workTime--;
										bAllowAdOff=1;
                }
            }
        }
    }
}



void PowerOff()
{
	 InitTriac();
    F_displayPowerOff();
    function=0;
    workTime=0;
    zuiXiaoWorkTime=0;
    zuiDaWorkTime=0;
    bPowerOnOff=0;
    flashFunction=0;
    flashYuYueTime=0;
    bFunctionSure=0;
    yuYueTime=0;
	buzzNum=0;
	bAllowAdOff=0;
 step=0;
 bFinish=0;
 keepTime=0;
}


void LedDisplay()
{

    if((bFunctionSure==0)&&(bSecFlash500ms==0)&&(function!=0))
    {
        displayBuffer[4]=0;
    }
    else
    {
        switch(function)
        {

        case   D_SuRe :
            F_setSuReL1() ;
            break;
        case   D_YangShengCha :
            F_setYangShengChaL2();
            break;
        case   D_TianPin  :
            F_setTianPinL3()	;
            break;
        case   D_ZhuZhou :
            F_setZhuZhouL4()	;
            break;
        case   D_DunTang :
            F_setDunTangL5();
            break;
        case   D_MenZhu  :
            F_setMenZhuL5()	;
            break;
        case   D_BaoWen  :
            F_setBaoWenL6()	;
            break;
        }
    }

}




